package com.huawei.tree.binary_tree_array;

//HeroNode节点——前中后序
public class HeroNode {

    private int no;
    private String name;
    private HeroNode left;//默认null
    private HeroNode right;//默认null

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //前序遍历的方法
    public void preOrder() {
        //先输出父节点
        System.out.println(this);
        //递归向左子树前序遍历
        if (this.left != null) {
            this.left.preOrder();
        }
        //递归向右子树前序遍历
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    //中序遍历的方法
    public void midOrder() {
        //递归向左子树中序遍历
        if (this.left != null) {
            this.left.midOrder();
        }
        //输出当前节点（父节点）
        System.out.println(this);
        //递归向右子树中序遍历
        if (this.right != null) {
            this.right.midOrder();
        }
    }

    //后序遍历的方法
    public void afterOrder() {
        //递归向左子树后序遍历
        if (this.left != null) {
            this.left.afterOrder();
        }
        //递归向右子树后序遍历
        if (this.right != null) {
            this.right.afterOrder();
        }
        //输出当前节点（父节点）
        System.out.println(this);
    }

    /**
     * @Name: 前序遍历查找
     * @Author: GaoJian
     * @Date: 2022/4/15 23:37
     * @Description: 如果找到就返回，如果没有找到就返回null
     */
    public HeroNode preOrderSearch(int no) {
        //比较一次
        System.out.println("比较一次");
        //1、比较当前节点是不是
        if (this.no == no) {
            return this;
        }
        //2、判断当前节点的左子树是否为空，如果不为空，则递归前序查找
        //3、如果左子树前序查找，找到节点，则返回
        HeroNode resNode= null;
        if (this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        //4、说明左子树找到
        if (resNode != null) {
            return resNode;
        }
        //5、判断右子树是否为空，不为空，则继续递归前序查找
        if (this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
        //6、返回右子树递归结果
        return resNode;
    }

    /**
     * @Name: 中序遍历查找
     * @Author: GaoJian
     * @Date: 2022/4/16 00:01
     * @Description: 如果找到就返回，没找到就返回null
     */
    public HeroNode midOrderSearch(int no) {
        //1、判断左子树是否为空
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.midOrderSearch(no);
        }
        //2、resNode为空，说明左子树没找到，判断当前节点
        if (resNode != null) {
            return resNode;
        }
        //比较一次
        System.out.println("比较一次");
        if (this.no == no) {
            return this;
        }
        //3、判断右子树是否为空
        if (this.right != null) {
            resNode = this.right.midOrderSearch(no);
        }
        //4、返回右子树查找结果
        return resNode;
    }

    /**
     * @Name: 后序遍历查找
     * @Author: GaoJian
     * @Date: 2022/4/16 00:16
     * @Description: 如果找到就返回，没找到就返回null
     */
    public HeroNode afterOrderSearch(int no) {
        //1、判断左子树是否为空
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.afterOrderSearch(no);
        }
        //2、resNode不为空则找到了
        if (resNode != null) {
            return resNode;
        }
        //3、判断右子树是否为空
        if (this.right != null) {
            resNode = this.right.afterOrderSearch(no);
        }
        //4、resNode不为空则找到了
        if (resNode != null) {
            return resNode;
        }
        //比较一次
        System.out.println("比较一次");
        //5、判断当前节点
        if (this.no == no) {
            return this;
        }
        //6、没找到返回null
        return null;
    }

    /**
     * @Name: 递归删除节点（规定1）
     * @Author: GaoJian
     * @Date: 2022/4/16 18:00
     * @Description:
     */
    public void delNode1(int no) {
        //1、判断是否是左子节点
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        //2、判断是否是右子节点
        if (this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }
        //3、递归删除左子树
        if (this.left != null) {
            this.left.delNode1(no);
        }
        //4、递归删除右子树
        if (this.right != null) {
            this.right.delNode1(no);
        }
    }

    /**
     * @Name: 递归删除节点（规定2）
     * @Author: GaoJian
     * @Date: 2022/4/16 18:28
     * @Description:
     */
    public void delNode2(int no) {
        //1、判断是否是左子节点
        if (this.left != null && this.left.no == no) {
            if (this.left.left != null) {
                this.left = this.left.left;
                return;
            }
            if (this.left.right != null) {
                this.left = this.left.right;
                return;
            }
        }
        //2、判断是否是右子节点
        if (this.right != null && this.right.no == no) {
            if (this.right.left != null) {
                this.right = this.right.left;
                return;
            }
            if (this.right.right != null) {
                this.right = this.right.right;
                return;
            }
        }
        //3、递归删除左子树
        if (this.left != null) {
            this.left.delNode2(no);
        }
        //4、递归删除右子树
        if (this.right != null) {
            this.delNode2(no);
        }
    }

}
